package com.example.hfweather

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.example.hfweather.adapter.ViewPagerAdapter
import com.example.hfweather.storage.TokenPreferences

class MainActivity : AppCompatActivity() {

    private var viewPager: ViewPager2? = null
    private var adapter: ViewPagerAdapter? = null

    //    默认显示的Fragment
    private var currentPosition: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.ac_main)

//        //测试token的获取
//        val token = TokenPreferences.getInstance(this).getToken()
//        Toast.makeText(this, token, Toast.LENGTH_SHORT).show()

//        添加点击事件
        addClickEvent()

//        加载Fragments
        loadFragments()

        // 恢复保存的状态
        if (savedInstanceState != null) {
            currentPosition = savedInstanceState.getInt("currentPosition", 0)
        }
        viewPager?.currentItem = currentPosition // 设置当前页面

    }


    /**
     * 加载Fragments
     */
    private fun loadFragments() {
//        1.获取viewPager2
        if (viewPager == null) {
            viewPager = findViewById(R.id.vpMain)

//            禁止滑动
            viewPager?.isUserInputEnabled = false
//            注册页面切换监听事件
            viewPager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
//                    a.改变底部导航栏的选中状态
                    setCurrentTab(position)
                    currentPosition = position
                }
            })
        }
//        2.创建Adapter
        if (adapter == null) {
            adapter = ViewPagerAdapter(this)

        }
//        3.设置Adapter
        viewPager?.adapter = adapter
        viewPager?.offscreenPageLimit = 2//缓存3个页面(0,1,2)


    }

    /**
     * 添加点击事件
     */
    private fun addClickEvent() {
//        1.获取控件
        findViewById<ViewGroup>(R.id.llBottomNavigation)
        val homeContainer = findViewById<View>(R.id.nav_home)
        val lifeContainer = findViewById<View>(R.id.nav_life)
        val profileContainer = findViewById<View>(R.id.nav_profile)
//        设置点击事件
        homeContainer.setOnClickListener {
            //a.实现页面切换（通过viewPage2实现）
            viewPager?.setCurrentItem(0, false)
            //b.改变底部导航栏的选中状态
            setCurrentTab(0)
        }
        lifeContainer.setOnClickListener {
            // a.实现页面切换（通过viewPage2实现）
            viewPager?.setCurrentItem(1, false)
            //b.改变底部导航栏的选中状态
            setCurrentTab(1)
        }
        profileContainer.setOnClickListener {
            //a.实现页面切换（通过viewPage2实现）
            viewPager?.setCurrentItem(2, false)
            //b.改变底部导航栏的选中状态
            setCurrentTab(2)
        }


    }

    /**
     * 设置当前选中的tab
     */
    private fun setCurrentTab(position: Int) {
        val bottomNavigationView = findViewById<ViewGroup>(R.id.llBottomNavigation)

        val tvHome = bottomNavigationView.findViewById<TextView>(R.id.tv_home)
        val imageHome = bottomNavigationView.findViewById<ImageView>(R.id.img_home)

        val tvLife = bottomNavigationView.findViewById<TextView>(R.id.tv_life)
        val imageLife = bottomNavigationView.findViewById<ImageView>(R.id.img_life)

        val tvProfile = bottomNavigationView.findViewById<TextView>(R.id.tv_profile)
        val imageProfile = bottomNavigationView.findViewById<ImageView>(R.id.img_profile)
        when (position) {
            0 -> {
                tvHome.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageHome.setColorFilter(resources.getColor(R.color.weather_primary, null))

                tvLife.setTextColor(resources.getColor(R.color.default_gray, null))
                imageLife.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvProfile.setTextColor(resources.getColor(R.color.default_gray, null))
                imageProfile.setColorFilter(resources.getColor(R.color.default_gray, null))
            }

            1 -> {
                tvHome.setTextColor(resources.getColor(R.color.default_gray, null))
                imageHome.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvLife.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageLife.setColorFilter(resources.getColor(R.color.weather_primary, null))

                tvProfile.setTextColor(resources.getColor(R.color.default_gray, null))
                imageProfile.setColorFilter(resources.getColor(R.color.default_gray, null))
            }

            2 -> {
                tvHome.setTextColor(resources.getColor(R.color.default_gray, null))
                imageHome.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvLife.setTextColor(resources.getColor(R.color.default_gray, null))
                imageLife.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvProfile.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageProfile.setColorFilter(resources.getColor(R.color.weather_primary, null))
            }
        }
    }
}